﻿using mozhi.smarterp.Business.Enums;
using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities;

namespace mozhi.smarterp.Business.Payments
{
    public class PaymentRequisition : AggregateRoot<Guid>
    {
        public DateTime Date { get; set; }

        public PaymentRequisitionType Type { private set; get; }

        public TradingPartnerType TradingPartnerType { private set; get; }

        public Guid? SupplierId { private set; get; }

        public Guid? EmployeeId { private set; get; }

        public Guid? CustomerId { private set; get; }

        public Guid CurrencyId { private set; get; }

        public decimal ExchangeRate { private set; get; }

        /// <summary>
        /// 应付金额
        /// </summary>
        public decimal Amount { private set; get; }

        /// <summary>
        /// 应付金额原币
        /// </summary>
        public decimal AmountFr { private set; get; }

        /// <summary>
        /// 付款金额
        /// </summary>
        public decimal? PaymentAmount { private set; get; }

        /// <summary>
        /// 付款金额原币
        /// </summary>
        public decimal? PaymentAmountFr { private set; get; }

        /// <summary>
        /// 是否预付款
        /// </summary>
        public bool IsAdvanced { private set; get; }

        public string Comment { get; private set; }

        public List<PaymentRequisitionItem> Items { private set; get; }

        public PaymentRequisition(Guid id,  DateTime date, PaymentRequisitionType type, TradingPartnerType tradingPartnerType, Guid? supplierId, Guid? employeeId, Guid? customerId, Guid currencyId,
           decimal exchangeRate, bool isAdvanced, DocumentStatusType status, string comment)
        {
            Id = id;

            Date = date;

            Type = type;
            
            TradingPartnerType = tradingPartnerType;

            SupplierId = supplierId;

            EmployeeId = employeeId;

            CustomerId = employeeId;

            CurrencyId = currencyId;

            ExchangeRate = exchangeRate;

            IsAdvanced = isAdvanced;

            Comment = comment;

            Items = new List<PaymentRequisitionItem>();
        }

        public void SetItems(List<PaymentRequisitionItem> items)
        {
            Items = items;
        }
    }

    public class PaymentRequisitionItem : Entity<Guid>
    {
        public decimal? PayableAmountFr { get; private set; }

        public decimal? PayableAmount { get; private set; }

        public decimal? RequisitionAmountFr { get; private set; }

        public decimal? RequisitionAmount { get; private set; }

        public DocumentType? SourceDocumentType { private set; get; }

        public Guid? SourceDocumentId { private set; get; }

        public Guid? SourceDocumentEntryId { private set; get; }

        public DateTime? DueDate { get; private set; }

        public DateTime? ExpectedPaymentDate { get; private set; }

        public string Remark { get; private set; }

        public PaymentRequisitionItem(Guid id, DocumentType? sourceDocumentType, Guid? sourceDocumentId, Guid? sourceDocumentEntryId, decimal? payableAmountFr, decimal? payableAmount, 
            decimal requisitionAmountFr, decimal? requisitionAmount,DateTime? dueDate, DateTime? expectedPaymentDate, string remark)
        {
            Id = id;

            SourceDocumentType = sourceDocumentType;
            SourceDocumentId = sourceDocumentId;
            SourceDocumentEntryId = sourceDocumentEntryId;

            PayableAmount = payableAmount;
            PayableAmountFr = payableAmountFr;

            RequisitionAmountFr = requisitionAmountFr;
            RequisitionAmount = requisitionAmount ?? 0;

            DueDate = dueDate;
            ExpectedPaymentDate = expectedPaymentDate;
            Remark = remark;
        }
    }
}
